import "./utils/dateFormat"  // 日期格式化
import {ThrottlerGuard, ThrottlerModule} from '@nestjs/throttler'
import {PrometheusModule} from 'nestjs-prometheus';

import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserModule } from './system/user/user.module';
import { AuthModule } from './auth/auth.module';
import { MenuModule } from './system/menu/menu.module';
import { RoleModule } from './system/role/role.module';
import { ConfigService, ConfigModule } from '@nestjs/config';
import envConfig from '../config/env';
import { APP_GUARD } from '@nestjs/core';
import { JwtAuthGuard } from './guard/jwt.guard'
import { AccountModule } from './account/account.module';
import { RoleMenuModule } from 'src/system/role-menu/role-menu.module';
import { DeptModule } from "./system/dept/dept.module";
import { DictDataModule } from "./system/dict-data/dict-data.module";
import { DictTypeModule } from "./system/dict-type/dict-type.module";
import { PostModule } from "./system/post/post.module";
import { UserPostModule } from "./system/user-post/user-post.module";
import { GenModule } from "./tool/gen/gen.module";
import { StudentModule } from "./system/student/student.module";
import {ImageUploadModule} from "./imageUpload/imageUpload.module"
import {ServerModule} from "./monitor/server/server.module"
import { HttpModule } from './http/http.module';
import { OnlineModule } from "./monitor/online/online.module";
import { CacheModule } from "./monitor/cache/cache.module";
import { PermissionGuard } from "./guard/permission.guard";

@Module({
  imports: [
    ThrottlerModule.forRoot([{
      ttl: 60000,  // 生存时间(毫秒)
      limit: 10,   // ttl 内的最大请求数
    }]),
    PrometheusModule.register(),
    ConfigModule.forRoot({ isGlobal: true, envFilePath: [envConfig.path] }),
    ConfigModule.forRoot({ isGlobal: true }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql', //数据库类型
        username: configService.get('DB_USER', 'user'),
        password: configService.get('DB_PASSWORD', '123456'),
        host: configService.get('DB_HOST', 'localhost'),
        port: configService.get('DB_PORT', 3306),
        database: configService.get('DB_DATABASE', 'vue3_admin'),
        entities: [__dirname + '/**/*.entity{.ts,.js}'], //实体文件
        synchronize: true, //synchronize字段代表是否自动将实体类同步到数据库
        retryDelay: 500, //重试连接数据库间隔
        retryAttempts: 10, //重试连接数据库的次数
        autoLoadEntities: true, //如果为true,将自动加载实体 forFeature()方法注册的每个实体都将自动添加到配置对象的实体数组中
      }),
    }),
    UserModule,
    AuthModule,
    MenuModule,
    RoleModule,
    AccountModule,
    RoleMenuModule,
    DeptModule,
    DictTypeModule,
    DictDataModule,
    PostModule,
    UserPostModule,
    GenModule,
    StudentModule,
    ImageUploadModule,
    ServerModule,
    HttpModule,
    OnlineModule,
    CacheModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // 启用全局身份校验，切记除了没有用@Public装饰器修饰的所有请求，都要携带token
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard
    },
    {
      provide: APP_GUARD,
      useClass: PermissionGuard,
    },
  ]
})
export class AppModule {  
  configure(consumer: MiddlewareConsumer) {}
}